<?php

class Test_Abovo_Menu extends Solar_Test {
    
    private $_menu;
    
    public function setup()
    {
        $this->_menu = Solar::factory('Abovo_Menu');
    }
    
    public function tearDown()
    {
        //$this->_menu = null;
    }
    
    public function testCanLoadFromCache()
    {
        
    }
    
    public function test_load()
    {
        $this->_menu->insert('Abovo_App_Crm_Companies');
        $this->_menu->insert('Abovo_App_Crm_Contacts');
        
        // unset menu object
        $this->_menu = null;
        
        $this->_menu = Solar::factory('Abovo_Menu');
        
        $this->assertTrue(isset($this->_menu->menu['Abovo_App_Crm']['apps']['Abovo_App_Crm_Companies']['name']));
        $this->assertSame(
            Solar::$locale->fetch('Abovo_App_Crm_Contacts', 'APP_ACTION_ADD'),
            $this->_menu->menu['Abovo_App_Crm']['apps']['Abovo_App_Crm_Contacts']['actions']['add']['name']
        );
    }
    
    public function testInsert()
    {
        // classes that have been recognized
        $apps = array(
            'Abovo_App_Crm_Contacts',
            'Abovo_App_Crm_Companies',
            'Abovo_App_System_Prefs',
        );
        
        // add some apps to the list of known apps
        foreach ($apps as $app) {
            // this should fetch info for the app and
            // refresh the cache accordingly
            $this->_menu->insert($app);
            $this->assertTrue($this->_menu->appExists($app));
        }
    }
    
    public function testAppExists()
    {
        $this->assertTrue($this->_menu->appExists('Abovo_App_Crm_Contacts'));
        $this->assertFalse($this->_menu->appExists('Some_App'));
    }
    
    public function testGetByhandle()
    {
        $this->skip('Better testing needed');
        
        $this->_perms = Solar::factory('Abovo_Model_Perms');
        
        // grant access to apps...
        $data = array(
            'handle' => 'antti',
            'type'         => 'handle',
            'class_name'   => 'Abovo_App_Crm_Companies',
            'action'       => 'add',
        );
        $this->_perms->grant($data);
        
        $data = array(
            'handle' => 'antti',
            'type'         => 'handle',
            'class_name'   => 'Abovo_App_Crm_Contacts',
            'action'       => 'add',
        );
        $this->_perms->grant($data);
        
        // grant an app that is not available
        $data = array(
            'handle' => 'antti',
            'type'         => 'handle',
            'class_name'   => 'Abovo_App_Crm_Calendar',
            'action'       => 'show',
        );
        
        $this->_perms->grant($data);
        
        $menu = array(
            array(
                'class' => 'Abovo_App_Crm',
                'name'  => 'CRM',
                'descr' => 'Customer Relationship Manager',
                'apps'  => array(
                    array(
                        'class' => 'Abovo_App_Crm_Companies',
                        'name'  => 'Companies',
                        'descr' => 'Manage companies',
                    ),
                ),
            ),
        );
        
        // this will fetch menu based on the permissions
        // that have been granted for a handle
        $menu = $this->_menu->getAllByHandle('antti');
        
        $this->assertTrue(count($menu) > 0);
        $this->assertTrue(isset($menu['Abovo_App_Crm']['name']));
    }
}
